package wca;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.ITurnSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * TestRamming tests if the robot can consistently ram the target.
 * 
 * @author calvin
 * 
 */

public class TestRamming extends RobotTestBed {

  // Variable for ramming target.
  private boolean rammedTarget = false;

  /**
   * The number of rounds to test.
   * 
   * @return 10 rounds
   * 
   */
  public int getNumRounds() {
    return 10;
  }

  /**
   * Determine if the robot is able to consistently ram the target.
   * 
   * @param e Turn Ended Event object
   */
  @Override
  public void onTurnEnded(TurnEndedEvent e) {

    // Get a snapshot of the turn.
    ITurnSnapshot snapshot = e.getTurnSnapshot();

    // Get all the active robots.
    IRobotSnapshot[] robots = snapshot.getRobots();

    // The robot has rammed the target.
    if (robots[1].getState().isHitRobot()) {
      rammedTarget = true;
    }
  }

  /**
   * Check if the robot has been able to win by ramming the target.
   * 
   * @param e Battle Completed Event object
   * 
   */
  public void onBattleCompleted(BattleCompletedEvent e) {
    assertTrue("Check that the robot has returned fire to where the target's bullet came from",
        rammedTarget);
    
    BattleResults[] results = e.getIndexedResults();
    BattleResults chinpoGumResults = results[1];
    String robotName = chinpoGumResults.getTeamLeaderName();

    assertEquals("Check that the winner is ChinpoGum", robotName, "wca.ChinpoGum*");
  }

  /**
   * Robots to be used for this test.
   * 
   * @return Robots
   * 
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,wca.ChinpoGum";
  }
}